#include "pm/dbobject.h"

using namespace db;

Object::Object()
: m_id(undefined)
{
}

Object::Object(wxSQLite3ResultSet& res)
: m_id(res.GetInt(wxT("id"), undefined))
{
}

Object::~Object()
{
}

ID Object::last_insert_id(wxSQLite3Database& db)
{
	return db.ExecuteScalar("select last_insert_rowid()");
}

bool Object::insert(wxSQLite3Database& db)
{
	if (valid()) {
		return false;
	}
	update_value_type res = prepare_insert(db);
	if (res.first == false) {
		return false;
	}
	if (res.second.ExecuteUpdate() != 1) {
		return false;
	}
	m_id = last_insert_id(db);
	return true;
}

bool Object::update(wxSQLite3Database& db)
{
	if (!valid()) {
		return false;
	}
	update_value_type res = prepare_update(db);
	return res.first && (res.second.ExecuteUpdate() == 1);
}
